package com.usian.service.lmpl;

import com.usian.mapper.RoomMapper;
import com.usian.room.Room;
import com.usian.roomuser.RoomUser;
import com.usian.service.RoomService;
import com.usian.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service

public class RoomServicelmpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void addRoom(Room room) {
        roomMapper.insert(room);
    }

    @Override
    public List<Room> roomList() {
        return roomMapper.roomList();
    }

    @Override
    public User getUserById(Integer roomUser) {
        return roomMapper.getUserById(roomUser);
    }
    @Override
    public List<RoomUser> getRoomUser() {
        List<RoomUser> roomUserList = redisTemplate.opsForList().range("roomUser", 0, -1);
        return roomUserList;
    }

    @Override
    public Room getRoomById(Integer roomUser, Integer roomNumber) {
        return roomMapper.getRoomById(roomUser, roomNumber);
    }

    @Override
    public void insertRoomUser(Room room1) {
        roomMapper.insertRoomUser(room1);
        List<RoomUser> roomUsers = roomMapper.roomUsers(room1.getId());
        for (RoomUser roomUsera : roomUsers) {
            //获取用户信息
            User users = roomMapper.getUserById(roomUsera.getUserId());
            //将用户信息推入Redis列表中
            if (users.getId().equals(room1.getRoomUser())) {
                redisTemplate.opsForList().rightPush(room1.getId() + "roomUser", users);
            }
        }
        List<RoomUser> roomUsersac = roomMapper.roomUser();
        redisTemplate.delete("roomUser");
        redisTemplate.opsForList().rightPush("roomUser", roomUsersac);
    }
    @Override
    public void delRoom(Integer roomid) {
        // 删除房间
        roomMapper.delRoom(roomid);
        // 删除房间用户
        roomMapper.delRoomUser(roomid);
        List<Room> roomList = roomMapper.roomList();
        // 清空旧数据
        redisTemplate.delete(roomid+"roomUser");
        redisTemplate.delete("roomUser");
        // 存储新数据
        List<Room> users = redisTemplate.opsForList().range("roomList", 0, -1);
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getId().equals(roomid)) {
                // 从Redis中删除该房间
                redisTemplate.opsForList().remove("roomList", 0, users.get(i));
                break; // 找到并删除后可以退出循环
            }
        }
        List<RoomUser> roomUsersac= roomMapper.roomUser();
        redisTemplate.opsForList().rightPush("roomUser", roomUsersac);
        // 删除房间消息
        redisTemplate.delete(roomid+"message");
    }

    @Override
    public void addUser(RoomUser roomUser) {
        List<RoomUser> roomUsersa = roomMapper.roomUsersId(roomUser.getRoomId(), roomUser.getUserId());
        if (roomUsersa.size()>0){
            for (RoomUser user : roomUsersa) {
                if (user.getUserId().equals(roomUser.getUserId())){
                    return;
                }
            }
        }
        //数据库添加用户
        roomMapper.addUser(roomUser);
//        //关闭新数据
        redisTemplate.delete("roomUser");

        //推入Redis
        List<RoomUser> roomUsers = roomMapper.roomUsers(roomUser.getRoomId());
        for (RoomUser roomUsera : roomUsers) {
            //获取用户信息
            User users = roomMapper.getUserById(roomUsera.getUserId());
            //将用户信息推入Redis列表中
            if (users.getId().equals(roomUser.getUserId())) {
                redisTemplate.opsForList().rightPush(roomUser.getRoomId() + "roomUser", users);
            }
        }
        List<RoomUser> roomUsersac= roomMapper.roomUser();
        redisTemplate.opsForList().rightPush("roomUser", roomUsersac);
        //设置键的过期时间为30分钟
        redisTemplate.expire(roomUser.getRoomId()+"roomUser", 30, TimeUnit.MINUTES);
    }

    @Override
    public List<User> getRoomUsers(Integer roomid) {
        //获取当前房间玩家人数
        List<User> roomUserList = redisTemplate.opsForList().range(roomid+"roomUser", 0, -1);
        return roomUserList;
    }

    @Override
/**
 * 从房间中删除用户
 * 此方法首先尝试从Redis缓存中删除用户信息，然后从数据库中删除用户信息
 * 这样做是为了确保数据的一致性，即使在缓存失效的情况下也能正确地从数据库中删除用户
 *
 * @param roomid 房间ID，表示用户所属的房间
 * @param userid 用户ID，表示需要从房间中删除的用户
 */
    public void delRoomUser(Integer roomid, Integer userid) {
        // 删除Redis中特定房间的所有用户信息的缓存，以确保数据一致性
//    redisTemplate.delete(roomid+"roomUser");
        // 删除Redis中名为"roomUser"的键，此处的逻辑可能需要进一步明确
        redisTemplate.delete("roomUser");

        // 从Redis缓存中获取房间内的所有用户列表
        List<User> Users = redisTemplate.opsForList().range(roomid+"roomUser", 0, -1);

        // 如果用户列表为空，则从数据库中重新获取用户列表
        if (Users.size()==0){
            Users = roomMapper.Users(roomid);
        }

        // 遍历用户列表，寻找并删除指定的用户
        for (int i = 0; i < Users.size(); i++) {
            // 当找到指定用户时，从数据库和Redis缓存中删除该用户
            if (Users.get(i).getId().equals(userid)){
                roomMapper.delRoomUsers(roomid,userid);
                redisTemplate.opsForList().remove(roomid+"roomUser",0,Users.get(i));
            }
        }

        // 再次调用数据库方法删除用户，以确保用户被正确删除
        roomMapper.delRoomUsers(roomid,userid);
        List<RoomUser> roomUsersac= roomMapper.roomUser();
        redisTemplate.opsForList().rightPush("roomUser", roomUsersac);
    }

    @Override
    public List<RoomUser> roomUsers() {
        return roomMapper.roomUser();
    }

}
